// 引入二次封装的 axios 实例
import http from "./http";

// 定义新增项目的接口函数
const projectAdd = (createData) => {
    // 发起 POST 请求到 /api/projects/add，携带项目数据
    return http.post('/api/projects/add', createData)
        .then(response => {
            // 处理响应数据
            return response;
        })
        .catch(error => {
            // 处理错误
            throw error;
        });
};
const getProject = () => {
    // 发起 GET 请求到 /api/projectsst
    return http.get('/api/projectsst')
        .then(response => {
            // 处理响应数据并
            return response;
        })
        .catch(error => {
            // 处理错误
            throw error;
        });
};
//可视化图表接口
const getProjectData = () => {
    return http.get('/api/projectData')
        .then(response => {
            return response.data;
        })
        .catch(error => {
            throw error;
        });
};

const getChatData = () => {
    return http.get('/api/projects/user')
        .then(response => {
            return response; // 返回 response.data
        })
        .catch(error => {
            console.error('Error fetching project data:', error); // 打印错误信息
            throw error;
        });
};
const getAllChatData = () => {
    return http.get('/api/projects/all')
        .then(response => {
            return response; // 返回 response.data
        })
        .catch(error => {
            console.error('Error fetching project data:', error); // 打印错误信息
            throw error;
        });
};

//ai接口
// 向后端发送问题
const sendChatQuestion = (question) => {
    // 后端改为接收请求体，因此直接发送 question 作为 JSON 数据
    const url = `/wenxin/chat`;

    return http.post(url, question, {
        headers: { 'Content-Type': 'application/json' }
    })
        .then((response) => response.data)
        .catch((error) => {
            console.error("发送问题失败:", error);
            throw error;
        });
};



// 获取后端返回的答案
const getChatAnswer = () => {
    return http.get("/wenxin/answer")
        .then(response => {
            return response; // 返回 response.data
        })
        .catch((error) => {
            console.error("获取答案失败:", error);
            throw error;
        });
};

//获取堆叠折线图的数据
const getLineChartData = (params) => {
    return http.get('/api/projects/output', { params }) // Axios 会自动将 params 转换为查询字符串
        .then(response=> {
            return response; // 返回 response.data
        })
        .catch(error => {
            console.error("获取图表数据失败:", error.response ? error.response.data : error.message);
            throw error;
        });
};

// 导出接口函数，供其他组件使用
export { projectAdd };
export { getProject };
export { getProjectData };
export { getChatData };
export { getAllChatData };
export { sendChatQuestion };
export { getLineChartData };
export { getChatAnswer };
